﻿namespace OboutInc.EasyMenu_Pro.Designer
{
    using OboutInc.EasyMenu_Pro;
    using System;
    using System.Text;
    using System.Web.UI.Design;

    public sealed class EasyMenuDesigner : ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            try
            {
                string str = string.Empty;
                EasyMenu component = (EasyMenu) base.Component;
                if (component.Components.Count == 0)
                {
                    return this.GetEmptyDesignTimeHtml();
                }
                StringBuilder builder = new StringBuilder();
                builder.Append("<link rel='stylesheet' type='text/css' href='" + ((component.StyleFolder != string.Empty) ? component.StyleFolder : "/emstyles/styles") + "/style.css'>");
                builder.Append("<div" + ((component.CSSMenu != string.Empty) ? (" class='" + component.CSSMenu + "'") : "") + " id='" + component.ID + "' style='width:" + component.Width + ";'>");
                builder.Append("<table cellpadding=0 cellspacing=0 border=0 width='100%' style='background-color:transparent;'>");
                if (component.Position == MenuPosition.Horizontal)
                {
                    builder.Append("<tr>");
                }
                for (int i = 0; i < component.Components.Count; i++)
                {
                    if (component.Position == MenuPosition.Vertical)
                    {
                        builder.Append("<tr>");
                    }
                    if (component.UseIcons && (component.IconsPosition == MenuIconsPosition.Left))
                    {
                        builder.Append("<td style=\"" + (((component.Position == MenuPosition.Vertical) || (i == 0)) ? "border-left:1px solid A7A6AA;" : "") + "border-bottom:1px solid A7A6AA; " + (((component.Position == MenuPosition.Horizontal) || (i == 0)) ? "border-top: 1px solid A7A6AA; " : "") + "font-size: 1px; width: 20px;\">&nbsp;</td>");
                    }
                    builder.Append("<td style=\"" + (((((component.Position == MenuPosition.Vertical) || (!component.UseIcons && (i == 0))) || (((component.Position == MenuPosition.Horizontal) && (component.IconsPosition == MenuIconsPosition.Left)) && component.UseIcons)) || (((i == 0) && (component.Position == MenuPosition.Horizontal)) && ((component.IconsPosition == MenuIconsPosition.Right) && component.UseIcons))) ? (((component.UseIcons && (component.IconsPosition == MenuIconsPosition.Left)) && !component.Components[i].AllowsIcon) ? "" : "border-left:1px solid A7A6AA;") : "") + (((component.UseIcons && (component.IconsPosition == MenuIconsPosition.Right)) && !component.Components[i].AllowsIcon) ? "" : "border-right:1px solid A7A6AA;") + "border-bottom:1px solid A7A6AA; " + (((component.Position == MenuPosition.Horizontal) || (i == 0)) ? "border-top: 1px solid A7A6AA; " : "") + "\">");
                    str = (component.Components[i].InnerHtml == string.Empty) ? component.Components[i].ID : component.Components[i].InnerHtml;
                    builder.Append("<div style=\"font:11px Tahoma; background-color:" + (((component.Components[i] is MenuItem) && ((MenuItem) component.Components[i]).Disabled) ? "DEDEDE" : "white") + "; padding:5px; \">" + str + "</div>");
                    builder.Append("</td>");
                    if (component.UseIcons && (component.IconsPosition == MenuIconsPosition.Right))
                    {
                        builder.Append("<td style=\"font-size: 1px; width: 20px;border-right:1px solid A7A6AA; border-bottom:1px solid A7A6AA; " + (((component.Position == MenuPosition.Horizontal) || (i == 0)) ? "border-top: 1px solid A7A6AA; " : "") + "\">&nbsp;</td>");
                    }
                    if (component.Position == MenuPosition.Vertical)
                    {
                        builder.Append("</tr>");
                    }
                }
                if (component.Position == MenuPosition.Vertical)
                {
                    builder.Append("</tr>");
                }
                builder.Append("</table>");
                builder.Append("</div>");
                return builder.ToString();
            }
            catch (Exception exception)
            {
                return this.GetErrorDesignTimeHtml(exception);
            }
        }

        protected override string GetEmptyDesignTimeHtml()
        {
            return base.CreatePlaceHolderDesignTimeHtml("Use <b>Components</b> property to create menu items.");
        }

        protected override string GetErrorDesignTimeHtml(Exception e)
        {
            return base.CreatePlaceHolderDesignTimeHtml(e.Message);
        }
    }
}

